package com.peanut.baby.mvp.profile;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.PTag;
import com.peanut.baby.model.User;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/22.
 */

public class ProfilePresenter implements ProfileContract.Presenter{

    private ProfileContract.View view;
    Activity    activity;
    public ProfilePresenter(ProfileContract.View view,Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getUserInfo(String userId) {
        Observable obs = RetrofitClient.getInstance().getFamousInfo(userId, InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<User>compose()).compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                view.onGetUserInfo(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetUserInfoFailed(msg);
            }
        });
    }

    @Override
    public void getTags(String userId) {
        Observable obs = RetrofitClient.getInstance().getSubscribedTagList(userId);
        obs.compose(BaseScheduler.<List<PTag>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<PTag>>() {
                    @Override
                    protected void onHandleSuccess(List<PTag> o) {
                        view.onSubscribedTagsGet(true,o,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onSubscribedTagsGet(false,null,msg);
                    }
                });
    }

    @Override
    public void changeFollowStatus(String targetId) {
        String token = InitManager.getInstance().getUser()==null?"":InitManager.getInstance().getUser().userToken;
        Observable obs = RetrofitClient.getInstance().changeFollowStatus(InitManager.getInstance().getUserId(),targetId,
                token);
        obs.compose(BaseScheduler.<User>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                view.onChangeStatusSuccess(o.isConcerned);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onChangeStatusFailed(msg);
            }
        });
    }
}
